package all.com.jinyongqunxiax.modules;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import java.util.ArrayList;
import java.util.List;

import all.com.jinyongqunxiax.R;
import all.com.jinyongqunxiax.model.mod.manager.Mod;
import all.com.jinyongqunxiax.model.mod.manager.ModManager;
import all.com.jinyongqunxiax.model.mod.SkillsExternalManager;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import jy.game.components.wuxue.pojo.JySkill;

public class SkillsExternalActivity extends FilterActivity {

    Options         options;
    SlimAdapter     adapter;
    List<ViewModel> viewModels       = new ArrayList<>();
    List<ViewModel> filterViewModels = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.skills_external_activity);
        options = new Options(getIntent());

        setTitle("外功");

        recyclerView = findViewById(R.id.skills_listView);
        // 设置布局管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // 设置Item添加和移除的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = SlimAdapter.create().register(R.layout.skills_external_skill_item, new SlimInjector<ViewModel>() {
                @Override
                public void onInject(@NonNull ViewModel viewModel, @NonNull IViewInjector injector) {
                    injector.text(R.id.skills_skill_item_title, viewModel.name);

                    injector.clicked(R.id.skills_skill_item_title, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = SkillExternalDetailActivity.Options.intent(SkillsExternalActivity.this, options.mod, viewModel.name);
                            startActivity(intent);
                        }
                    });
                }
            }
        ).attachTo(recyclerView);

        loadNewData();
    }

    @Override
    void loadNewData() {
        Mod                   mod     = ModManager.shared().get(options.mod);
        SkillsExternalManager manager = mod.skillsExternalManager;
        for (JySkill skill : manager.list) {
            viewModels.add(new ViewModel(skill));
        }
        setTitle("外功#" + (viewModels.size()));
        adapter.updateData(viewModels);
    }

    @Override
    void filter(String text) {
        text = text.trim().toLowerCase();
        if (text.isEmpty()) {
            setTitle("外功#" + (viewModels.size()));
            adapter.updateData(viewModels);
        } else {
            filterViewModels.clear();

            for (ViewModel viewModel : viewModels) {
                if (viewModel.name.toLowerCase().contains(text)) {
                    filterViewModels.add(viewModel);
                }
            }
            setTitle("外功 " + (filterViewModels.size()));
            adapter.updateData(filterViewModels);
        }
    }

    public static class Options {
        String mod;
        String component;

        Options(Intent intent) {
            this.mod = intent.getStringExtra("mod");
            this.component = intent.getStringExtra("component");
        }

        static Intent intent(Context context, String mod, String component) {
            Intent intent = new Intent(context, SkillsExternalActivity.class);

            intent.putExtra("mod", mod);
            intent.putExtra("component", component);
            return intent;
        }
    }

    private static class ViewModel {
        String  name;
        JySkill skill;

        ViewModel(JySkill skill) {
            this.skill = skill;
            this.name = skill.name;
        }
    }
}
